home *** CD-ROM | disk | FTP | other *** search
- Path: news.iadfw.net!usenet
- From: jakramer@airmail.net (JA Kramer)
- Newsgroups: comp.lang.c
- Subject: Re: Need to get value from 2-D matrix
- Date: Thu, 04 Apr 1996 17:35:30 GMT
- Organization: customer of Internet America
- Message-ID: <4k0psu$lit@news-f.iadfw.net>
- References: <DotDpo.6o2@news.uwindsor.ca>
- NNTP-Posting-Host: dal17-20.ppp.iadfw.net
- X-Newsreader: Forte Free Agent v0.55
-
- kima@uwindsor.ca (Sam Kim) wrote:
-
-
- >I'm writing a program that reads in a 2-D matrix from binary. I am
- >useless with structures, so I'm trying to do this with arrays and
- >pointers.
-
- >Question : How do I read in the values?
-
- >I have :
-
- >for (i=0; i<n; i++)
- > for (j=0; j<n; j++)
- > {
- > fread(&ptr,1,1,fp)
- > matrix[i][j] = ptr;
- > ptr++;
- > }
-
- >Shouldn't this work?
-
- Sam, you really haven't given enough information to fully evaluate
- your code segment. Here's a few questions/comments you may find
- helpful.
-
- 1) What is the binary format? Were the original matrix values written
- as integers (long or short) or floats (single or double)? In order to
- read the data correctly you will need to use the same data type. (I'll
- assume that both the writing and reading CPUs are the same and you
- don't need to worry about Little/Big Endians!)
-
- 2) Is the file itself formatted in any way? I.e. was the matrix
- written out by rows with each row delimited by CR/LF?
-
- 3) Given your code segment, "ptr" only needs to be the size of one
- matrix element and not a buffer. If this was your intent then "ptr++"
- is walking over the rest of your program!
-
- 4) Here's your most efficient implementation given that the person who
- wrote the matrix acted accordingly and that the underlying data value
- was an integer.
-
- int matrix[N][M];
- .
- .
- .
- fread (matrix, sizeof( int), N*M, fp);
- .
- .
- .
-
- It doesn't get much simpler, or faster, than this. Don't forget to add
- error handling. Note also, that with DOS I believe that there's a
- limit of 64Kbytes that you can read at one time. Also, you'll need to
- open the file appropriately.
-
-